package com.rightsidetech.http.controller.storeApp;

import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.system.dtos.request.AppVersionReq;
import com.rightsidetech.system.dtos.response.AppVersionResp;
import com.rightsidetech.system.service.AppUpdateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.regex.Pattern;

/**
 * @Author: yhr
 * @Date: 2021-11-09 16:05
 */
@RestController
@RequestMapping("/storeApp/app_update")
@ShenyuSpringMvcClient(path = "/storeApp/app_update/**")
@Api(tags = "门店app版本升级相关")
public class StoreAppUpdateController {
    @Resource
    private AppUpdateService appUpdateService;

    @Log
    @GetMapping("/versionDate")
    @ApiOperation(value = "获取用户app最新版本")
    public ResponseData<AppVersionResp> versionDate(AppVersionReq req) throws Exception {
        boolean isMatch = Pattern.matches("\\d+(\\.\\d+)(\\.\\d+)", req.getVersion());
        if(!isMatch){
            return ResponseData.fail("版本号格式不合法");
        }
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return appUpdateService.versionDate(req.getVersion(),req.getAppOs(),req.getAppType());
    }
}
